﻿using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Truron.Purchase.Model.RemoteModel;
using Truron.Purchase.Utility.RedisHelper;

namespace Truron.Purchase.Api.Utility
{
    public class SysCenterServiceHelp
    {
        private static string sysCenterApiUrl = "";//系统服务调用地址
        public readonly static SysCenterServiceHelp Instance = new SysCenterServiceHelp();
        public SysCenterServiceHelp()
        {
            var Configuration = new ConfigurationBuilder()
                  .SetBasePath(Directory.GetCurrentDirectory())
                  .AddJsonFile("appsettings.json", optional: true)
                  .Build();
            sysCenterApiUrl = Configuration.GetSection("AppSettings:SysCenterApiUrl").Value;

        }
        /// <summary>
        /// 获取用户登录信息
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static UserCache GetUser(HttpContext context)
        {
            var token = context.Request.Headers["Authorization"].ToString();
            return RedisBase.Item_Get<UserCache>(token);
        }
        /// <summary>
        /// 获取全部用户信息
        /// </summary>
        /// <returns></returns>
        public (int code, List<SysCenterUserModel> list) GetUserAll(UserCache userToken)
        {
            var userListObj = ApiHelp.WebPost(sysCenterApiUrl + "SysUser/GetUserAll", userToken.UserToken);
            if (userListObj.code > 0)
                return (userListObj.code, null);
            return (userListObj.code, JsonConvert.DeserializeObject<List<SysCenterUserModel>>(userListObj.data.ToString()));

        }
        /// <summary>
        /// 获取全部用户信息，带权限管理的
        /// </summary>
        /// <param name="userToken">用户登录缓存信息</param>
        /// <returns></returns>
        public (int code, List<SysCenterUserModel> list) GetUserAllByPower(UserCache userToken)
        {
            Dictionary<string, object> postData = new Dictionary<string, object>();
            postData.Add("userId", userToken.UserId);
            var userListObj = ApiHelp.WebPost(sysCenterApiUrl + "SysUser/GetUserAllByPower", userToken.UserToken, postData);
            if (userListObj.code > 0)
                return (userListObj.code, null);
            return (userListObj.code, JsonConvert.DeserializeObject<List<SysCenterUserModel>>(userListObj.data.ToString()));
        }
        /// <summary>
        /// 根据部门获取部门下的所有用户ID集合
        /// </summary>
        /// <param name="userToken"></param>
        /// <param name="deptId"></param>
        /// <returns></returns>
        public (int code, List<int> ids) GetUserByDept(UserCache userToken, int deptId)
        {
            Dictionary<string, object> postData = new Dictionary<string, object>();
            postData.Add("deptId", deptId);
            var userSearchObj = ApiHelp.WebPost(sysCenterApiUrl + "SysUser/GetUserByDept", userToken.UserToken, postData);
            if (userSearchObj.code > 0)
                return (userSearchObj.code, null);
            return (userSearchObj.code, JsonConvert.DeserializeObject<List<int>>(userSearchObj.data.ToString()));
        }
        /// <summary>
        /// 根据用户名搜索用户ID列表
        /// </summary>
        /// <param name="userToken"></param>
        /// <param name="searchKey"></param>
        /// <returns></returns>
        public (int code, List<int> ids) GetUserByRealName(UserCache userToken, string searchKey)
        {
            Dictionary<string, object> postData = new Dictionary<string, object>();
            postData.Add("searchKey", searchKey);
            var userSearchObj = ApiHelp.WebPost(sysCenterApiUrl + "SysUser/GetUserByRealName", userToken.UserToken, postData);
            if (userSearchObj.code > 0)
                return (userSearchObj.code, null);
            return (userSearchObj.code, JsonConvert.DeserializeObject<List<int>>(userSearchObj.data.ToString()));
        }
        /// <summary>
        /// 获取用户实体信息
        /// </summary>
        /// <param name="userToken"></param>
        /// <param name="userId">用户Id</param>
        /// <returns></returns>
        public (int code, SysCenterUserModel userEnt) GetUserDetail(UserCache userToken, int userId)
        {
            Dictionary<string, object> postData = new Dictionary<string, object>();
            postData.Add("userId", userId);
            var userEntObj = ApiHelp.WebPost(sysCenterApiUrl + "SysUser/GetUserDetail", userToken.UserToken, postData);
            if (userEntObj.code > 0)
                return (userEntObj.code, null);
            return (userEntObj.code, JsonConvert.DeserializeObject<SysCenterUserModel>(userEntObj.data.ToString()));
        }
        /// <summary>
        /// 根据用户获取部门下的所有用户Id
        /// </summary>
        /// <param name="userToken"></param>
        /// <param name="userId">用户Id</param>
        /// <returns></returns>
        public (int code, List<SysCenterUserModel> userList) GetManagerUser(UserCache userToken, int userId)
        {
            Dictionary<string, object> postData = new Dictionary<string, object>();
            postData.Add("userId", userId);
            var userEntObj = ApiHelp.WebPost(sysCenterApiUrl + "SysUser/GetManagerUser", userToken.UserToken, postData);
            if (userEntObj.code > 0)
                return (userEntObj.code, null);
            return (userEntObj.code, JsonConvert.DeserializeObject<List<SysCenterUserModel>>(userEntObj.data.ToString()));
        }
        /// <summary>
        /// 根据用户获取上级列表(直到顶级)
        /// </summary>
        /// <param name="userToken"></param>
        /// <param name="userId">用户Id</param>
        /// <returns></returns>
        public (int code, List<SysCenterUserModel> userList) GetLeaderUser(UserCache userToken, int deptId)
        {
            Dictionary<string, object> postData = new Dictionary<string, object>();
            postData.Add("deptId", deptId);
            var userEntObj = ApiHelp.WebPost(sysCenterApiUrl + "SysUser/GetLeaderUser", userToken.UserToken, postData);
            if (userEntObj.code > 0)
                return (userEntObj.code, null);
            return (userEntObj.code, JsonConvert.DeserializeObject<List<SysCenterUserModel>>(userEntObj.data.ToString()));
        }

        /// <summary>
        /// 获取业务单元数据源（下拉列表绑定数据源使用  特定格式）
        /// </summary>
        /// <param name="userToken"></param>
        /// <param name="userId">用户Id</param>
        /// <returns></returns>
        public (int code, object sourceList) GetSourceSelect(UserCache userToken)
        {
            var sourceObj = ApiHelp.WebPost(sysCenterApiUrl + "SysDataSource/GetSelect", userToken.UserToken);
            if (sourceObj.code > 0)
                return (sourceObj.code, null);
            return (sourceObj.code, sourceObj);
        }
        /// <summary>
        /// 获取业务单元数据源
        /// </summary>
        /// <param name="userToken"></param>
        /// <param name="userId">用户Id</param>
        /// <returns></returns>
        public (int code, List<SysCenterSourceModel> sourceList) GetSourceList(UserCache userToken)
        {
            var sourceObj = ApiHelp.WebPost(sysCenterApiUrl + "SysDataSource/GetDataSource", userToken.UserToken);
            if (sourceObj.code > 0)
                return (sourceObj.code, null);
            return (sourceObj.code, JsonConvert.DeserializeObject<List<SysCenterSourceModel>>(sourceObj.data.ToString()));
        }
    }
}
